package com.wanmao.test.shebei;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class SQLdm {
    private Context mContext;

    public SQLdm(Context mContext) {
        this.mContext = mContext;
    }

    String filePath = "data/data/com.wanmao.test/AS/EXCEL.db";

    String pathStr = "data/data/com.wanmao.test/AS";

    SQLiteDatabase database;

    public SQLiteDatabase openDatabase() {


        File jhPath = new File(filePath);

        if (jhPath.exists()) {
            return SQLiteDatabase.openOrCreateDatabase(jhPath, null);
        } else {
            boolean sdExist = android.os.Environment.MEDIA_MOUNTED
                    .equals(android.os.Environment.getExternalStorageState());
            if (!sdExist) {
                return null;
            }

            try {

                AssetManager am = mContext.getAssets();
                InputStream is = am.open("EXCEL.db");

                FileOutputStream fos = new FileOutputStream(jhPath);
                byte[] buffer = new byte[1024];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.flush();
                fos.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            return openDatabase();
        }
    }

    public List<ExcelInfo> getMainInfo(SQLiteDatabase sqliteDB) {
        Cursor cursor = sqliteDB.rawQuery("select * from EXCELInfo where FSBLXID=''", null);
        List<ExcelInfo> infoList = new ArrayList<>();
        ExcelInfo info = null;
        try {
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    String OBJ_ID = cursor.getString(cursor.getColumnIndex("OBJ_ID"));
                    String SBLXBM = cursor.getString(cursor.getColumnIndex("SBLXBM"));
                    String SBLX = cursor.getString(cursor.getColumnIndex("SBLX"));
                    String FSBLXID = cursor.getString(cursor.getColumnIndex("FSBLXID"));
                    info = new ExcelInfo(OBJ_ID, SBLXBM, SBLX, FSBLXID);
                    infoList.add(info);

                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (SQLiteException e) {
            Log.e("error", "error", e);
        } catch (Exception e) {
            Log.e("error", "error", e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (sqliteDB != null) {
                sqliteDB.close();
            }
        }
        return infoList;
    }

    public List<ExcelInfo> getSubInfo(SQLiteDatabase sqliteDB, String Fid) {

        Cursor cursor = sqliteDB.rawQuery("select * from EXCELInfo where FSBLXID = ?", new String[]{Fid});

        List<ExcelInfo> infoList = new ArrayList<>();
        ExcelInfo info = null;
        try {
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    String OBJ_ID = cursor.getString(cursor.getColumnIndex("OBJ_ID"));
                    String SBLXBM = cursor.getString(cursor.getColumnIndex("SBLXBM"));
                    String SBLX = cursor.getString(cursor.getColumnIndex("SBLX"));
                    String FSBLXID = cursor.getString(cursor.getColumnIndex("FSBLXID"));
                    info = new ExcelInfo(OBJ_ID, SBLXBM, SBLX, FSBLXID);
                    infoList.add(info);
                } while (cursor.moveToNext());
            }
            cursor.close();
        } catch (SQLiteException e) {
            Log.e("error", "error", e);
        } catch (Exception e) {
            Log.e("error", "error", e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (sqliteDB != null) {
                sqliteDB.close();
            }
        }
        return infoList;
    }


}
